package com.laicunba.service.accounting.impl;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.laicunba.pojo.CurrentAccountItemDO;
import com.laicunba.pojo.FinancingTargetDO;
import com.laicunba.pojo.UserDO;
import com.laicunba.service.CurrentAccountService;
import com.laicunba.service.FinancingTargetService;
import com.laicunba.service.SaleService;
import com.laicunba.service.UserService;
import com.laicunba.service.accounting.AccountingTemplate;
import com.laicunba.service.accounting.CurrentAccount2FixService;
import com.laicunba.util.AssertUtil;
import com.laicunba.util.BigDecimalUtil;
import com.laicunba.util.SeqUtil;
import com.laicunba.util.StringUtil;

@Service
public class CurrentAccount2FixServiceImpl extends AccountingTemplate implements CurrentAccount2FixService {
	@Autowired
	private UserService userService;
	@Autowired
	private FinancingTargetService financingTargetService;
	@Autowired
	private CurrentAccountService currentAccountService;
	@Autowired
	private SaleService saleService;

	@Transactional
	@Override
	public String doTransfor(String uid, String financingTargetId, BigDecimal amount, boolean transferCurrent, String cashCouponId, String rateIncrCouponId) {
		AssertUtil.assertTrue(StringUtil.isNotEmpty(uid, financingTargetId), "PARAM_ERROR");
		UserDO userDO = userService.get(UserDO.class, uid);
		AssertUtil.assertNotNull(userDO, "USER_NOT_EXIST");
		AssertUtil.assertNotNull(amount, "AMOUNT_IS_NULL");
		AssertUtil.assertTrue(amount.compareTo(BigDecimal.valueOf(0)) > 0, "AMOUNT_LESS_0");
		BigDecimal balance = currentAccountService.getBalance(uid);
		AssertUtil.assertNotNull(balance, "CURRENT_ACCOUNT_BALANCE_NOT_EXIST");
		AssertUtil.assertTrue(balance.compareTo(amount) >= 0, "BALANCE_NOT_ENOUGH");
		FinancingTargetDO financingTargetDO = financingTargetService.get(FinancingTargetDO.class, financingTargetId);
		AssertUtil.assertNotNull(financingTargetDO, "FINANCING_TARGET_NOT_EXIST");
		AssertUtil.assertNotNull(financingTargetDO.getFinishAmount(), "FINANCING_TARGET_FINISH_AMOUNT_NULL");
		AssertUtil.assertNotNull(financingTargetDO.getCollectAmount(), "FINANCING_TARGET_COLLECT_AMOUNT_NULL");
		AssertUtil.assertNotNull(financingTargetDO.getMinAmount(), "FINANCING_TARGET_MIN_AMOUNT_NULL");
		AssertUtil.assertTrue(amount.compareTo(financingTargetDO.getMinAmount()) >= 0, "AMOUNT_LESS_MIN");
		AssertUtil.assertTrue("CPB".equalsIgnoreCase(financingTargetDO.getType()), "FINANCING_TARGET_NOT_CPB");
		AssertUtil.assertTrue("onsale".equalsIgnoreCase(financingTargetDO.getStatus()), "FINANCING_TARGET_NOT_ONSALE");
		AssertUtil.assertTrue(financingTargetDO.getCollectAmount().subtract(financingTargetDO.getFinishAmount(), BigDecimalUtil.MC).compareTo(amount) >= 0, "FINANCING_TARGET_STOCK_NOT_ENOUGH");

		// 减少活期余额
		CurrentAccountItemDO currentAccountItemDO = currentAccountService.subtract(uid, amount, "CA2D", "target_id:" + financingTargetId);
		AssertUtil.assertNotNull(currentAccountItemDO, "H2D_FAIL");

		// 购买定标
		String certificateId = "CA2D-" + StringUtil.genGUID();
		SaleService.MakeFinancingTargetBidParam lockParam = new SaleService.MakeFinancingTargetBidParam();
		lockParam.setCertificateId(certificateId);
		lockParam.setFinancingTargetId(financingTargetDO.getId());
		lockParam.setBuyAmount(amount);
		lockParam.setBuyUserId(userDO.getId());
		lockParam.setCashCouponId(cashCouponId);
		lockParam.setTransferCurrent(transferCurrent);
		lockParam.setRateIncrCouponId(rateIncrCouponId);
		lockParam.setSummary("ca_item_id:" + currentAccountItemDO.getId());
		lockParam.setSeq(SeqUtil.genNewBidSeq());
		AssertUtil.assertTrue(saleService.makeFinancingTargetBid(lockParam), "LOCK_ERROR");

		SaleService.PayedFinancingTargetParam payedParam = new SaleService.PayedFinancingTargetParam();
		payedParam.setCertificateId(certificateId);
		payedParam.setPayMark("DEMAND_2_FIX");
		// payedParam.settN(0);
		AssertUtil.assertTrue(saleService.payedFinancingTarget(payedParam), "PAYED_ERROR");

		return certificateId;
	}

}
